package sanshui.system.trade.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import sanshui.system.trade.common.CommonResponse;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.db.entity.TradeTargetCode;
import sanshui.system.trade.db.entity.TradeTargetCodeExample;
import sanshui.system.trade.db.mapper.TradeTargetCodeMapper;
import sanshui.system.trade.factory.data.DataFactory;
import sanshui.system.trade.pojo.StockInfoVo;
import sanshui.system.trade.pojo.ao.QueryAccAo;
import sanshui.system.trade.pojo.ao.TargetCodeAo;
import sanshui.system.trade.service.ITradeTargetCodeService;
import sanshui.system.trade.util.CommonTimeUtil;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/data")
@Slf4j
public class TradeDataController {

    @Resource
    DataFactory dataFactory;
    @Resource
    ITradeTargetCodeService targetCodeService;
    @Resource
    TradeTargetCodeMapper targetCodeMapper;

    @ApiOperation(value = "获取历史数据")
    @GetMapping("/getHisData/{code}/{market}/{startDate}/{endDate}")
    public CommonResponse<List<StockInfoVo>> getHisData(@PathVariable("code") String code,
                                                        @PathVariable("market") String market,
                                                        @PathVariable("startDate") String startDate,
                                                        @PathVariable("endDate") String endDate) {
        log.info("获取历史数据, code:{}, startDate:{}, endDate:{}", code, startDate, endDate);
        code = code + "." + market.toUpperCase();
        return CommonResponse.ok(dataFactory.getHisStockInfo(code, startDate, endDate));
    }

    @ApiOperation(value = "删除持仓数据")
    @GetMapping("/deletePos/{accId}/{code}")
    public CommonResponse<List<StockInfoVo>> deletePos(@PathVariable("accId") String accId,
                                                       @PathVariable("code") String code
                                                       ) {
        log.info("删除持仓数据, accId:{}, code:{}", accId, code);
        return CommonResponse.ok(null);
    }

    @ApiOperation(value = "批量添加自选")
    @PostMapping("/addChooseBatch")
    public CommonResponse<List<TradeTargetCode>> addChoose(@RequestBody TargetCodeAo targetCodeAo) {
        log.info("批量添加自选, targetCodeAo:{}, ", targetCodeAo);
        if (Strings.isNotEmpty(targetCodeAo.getTargetCodes())){
            String[] split = targetCodeAo.getTargetCodes().split(",");
            for (String tsCode : split) {
                targetCodeService.insert(tsCode);
            }
        }
        if (Strings.isNotEmpty(targetCodeAo.getIndex())){
            targetCodeService.insertIndex(targetCodeAo.getIndex());
        }

        TradeTargetCodeExample example = new TradeTargetCodeExample();
        TradeTargetCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        return CommonResponse.ok(targetCodeMapper.selectByExample(example));
    }

    @ApiOperation(value = "添加交易")
    @GetMapping("/addChoose/{tsCode}")
    public CommonResponse<List<TradeTargetCode>> addChoose(@PathVariable("tsCode") String tsCode) {
        log.info("添加自选, tsCode:{}, ", tsCode);
        targetCodeService.insert(tsCode);
        TradeTargetCodeExample example = new TradeTargetCodeExample();
        TradeTargetCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        return CommonResponse.ok(targetCodeMapper.selectByExample(example));
    }

    @ApiOperation(value = "删除自选")
    @GetMapping("/delChoose/{tsCode}")
    public CommonResponse<List<TradeTargetCode>> delChoose(@PathVariable("tsCode") String tsCode) {
        log.info("删除自选, tsCode:{}, ", tsCode);
        TradeTargetCode tradeTargetCode = new TradeTargetCode();
        tradeTargetCode.setTsCode(tsCode);
        tradeTargetCode.setDelFlg(TradeType.DelFlg.DELETE.getCode());
        targetCodeService.update(tradeTargetCode);

        TradeTargetCodeExample example = new TradeTargetCodeExample();
        TradeTargetCodeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        return CommonResponse.ok(targetCodeMapper.selectByExample(example));
    }

}
